<?php
session_start();
require("../bin/common.php");

//Check if install mode is not running
$installdata = file_get_contents("../lib/installdata.txt") or exit("Cannot open installdata.txt");
if ($installdata == "true") {
	checkSess();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Setup Control Panel - Vanadium Patch Mananger</title>
<link href="../lib/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../lib/script.js"></script>
</head>
<body onload="fixSize()" onresize="fixSize()">

<div class="container" id="container">

	<!-- Header -->
	<div class="top">
	Vanadium Patch Manager

	<?php
	dispLogin();
	?>
	</div>
    
	<!-- Left Hand Panel -->
	<div class="left">
		<br /><span class="nav_option"><img src="../lib/gfx/icon_home.gif" width="15" height="15" alt="Home" /> <a href="../home/">Home</a></span><br /><br />
		<span class="nav_tab_option"><img src="../lib/gfx/icon_add.gif" width="12" height="12" alt="Add" /> <a href="../addpatch/">Add Patch</a></span><br /><br />
		<span class="nav_tab_option"><img src="../lib/gfx/icon_edit.gif" width="13" height="16" alt="Edit" /> <a href="../patch/changelog.php">View Changelog</a></span><br /><br />
		<span class="nav_tab_option"><img src="../lib/gfx/icon_edit.gif" width="13" height="16" alt="Edit" /> <a href="../editpatch/">Edit Patch Home</a></span><br /><br />
		<span class="nav_tab_option"><img src="../lib/gfx/icon_setup.gif" width="14" height="16" alt="Setup" /> <a href="../setup/">Setup</a></span><br /><br />
        <span class="nav_tab_tab_option"><img src="../lib/gfx/icon_edit.gif" width="13" height="16" alt="Edit" /> <a href="../setup/neoncube.php">Neoncube.ini</a></span><br /><br />
        <span class="nav_tab_tab_option"><img src="../lib/gfx/icon_account.gif" width="14" height="14" alt="Accounts" /> <a href="../setup/accounts.php">Accounts</a></span><br /><br />
        <span class="nav_tab_tab_option"><img src="../lib/gfx/icon_cp.gif" width="16" height="16" alt="Control Panel" /> <a href="../setup/cpanel.php">Control Panel</a></span>
	</div>
    
	<!-- Body Content Container -->
	<div class="content_container" id="content_container">
    
		<!--Content Title -->
		<div class="content_title">
			Setup - Control Panel
		</div>
        
		<!--Content Body -->
		<div class="content_body">
			<?php
			//Get Variables
			$prevpatch = $_POST['previouspatch'];
			if ($prevpatch == "yes") {
				$patch_no = $_POST['patch_no'];
			}
			$user = $_POST['username'];
			$pass = $_POST['password'];
			
			if (isset($user) and isset($pass) and isset($prevpatch)) {
				//Add Accounts to /lib/accounts.php
				$fp = fopen("../lib/accounts.php","w");
											
				if(!$fp) {
					echo 'Error: Cannot open accounts.php file.';
					exit;
				}
				
				fwrite($fp, "<?php\n#".$user."||".$pass."\n?>");
				
				fclose($fp);
				
				
				if ($prevpatch == "yes" and isset($patch_no)) {
					//Edit patchlist.txt
					$fp = fopen("../patch/patchlist.txt","w");
												
					if(!$fp) {
						echo 'Error: Cannot open patchlist.txt file.';
						exit;
					}
					
					//Remove double line formatting
					$patch_no = str_replace("\r\n","\n", $patch_no);
					
					fwrite($fp, $patch_no);
					
					fclose($fp);
					
					
					//Edit patchdata.txt
					$fp = fopen("../lib/patchdata.txt","w");
												
					if(!$fp) {
						echo 'Error: Cannot open patchdata.txt file.';
						exit;
					}
					
					//Seperate into lines
					$sepline = explode("\r\n",$_POST['patch_no']);
					
					for ($count = 0; $count < count($sepline); $count++) {
						$patch_var[$count] = explode("	", $sepline[$count]);
					}
					
					for ($count = 0; $count < count($sepline); $count++) {
						$patchdata .= "".$patch_var[$count][0]."||".time()."||".$user."||Imported from installation.\n";
					}
					
					$patchdata = substr($patchdata,0,-1);
					
					fwrite($fp, $patchdata);
					
					fclose($fp);
					
					echo "Notice: Since you selected that you had patches previously, you must add the patches to your <b>".substr($_SERVER['PHP_SELF'],0,-17)."patch/</b> directory <b>or your patcher will not work!</b>.<br /><br />";
				}
				
				
				//Edit /lib/installdata.txt
				$fp = fopen("../lib/installdata.txt","w");
											
				if(!$fp) {
					echo 'Error: Cannot open installdata.txt file.';
					exit;
				}
				
				fwrite($fp, "true");
				
				fclose($fp);
				
			}
			?>
            You may now log in with your desired username/password.<br /><br />
            If you wish to review any settings or add any accounts please visit the Setup page.
        </div>
        
	<!--End Content Container-->
	</div>
    
	<!--Footer -->
	<div class="footer">
		Vanadium Patch Manager, A Neoncube Patch Modification.<br />
		GNU General Public License v3 - 2010<br />
		http://code.google.com/p/vanadiumpatch/
	</div>

<!--End Container-->
</div>
</body>
</html>